import 'package:flutter/material.dart';
import 'package:hao_ke/utils/dio_http.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import '../auth/auth.dart';
import '../utils/scoped_model_helper.dart';

class ProjectEmployee extends StatefulWidget {

  final String pkid;
  const ProjectEmployee( this.pkid, {Key? key}) : super(key: key);
  @override
  State<ProjectEmployee> createState() => _ProjectEmployeeState();
}

class _ProjectEmployeeState extends State<ProjectEmployee> {

  List<Widget> list = [];

  String employeeId = '';

  List<String> employeeList = [];
  List<String> employeeIdList = [];
  List<String> storeIdList = [];


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: const Text('项目员工'),
      ),
      body: Container(
        padding: const EdgeInsets.all(10),
        decoration: const BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  Color.fromRGBO(200, 100, 100, 0.5),
                  Color.fromRGBO(0, 0, 200, 0.5),
                ]
            )
        ),
        child: ListView(
          children: list
        ),
      ),
      floatingActionButton: IconButton(
        icon: const Icon(Icons.add_circle_outline,size: 50,color: Colors.black54,),
        onPressed: (){
          TDPicker.showMultiPicker(context, title: '选择护理师',
              pickerHeight: 300,
              onConfirm: (selected) {
                DioHttp()
                    .init(context, ScopedModelHelper.getModel<AuthModel>(context).token)
                    .post("/oms/project-employee/add?projectId=${widget.pkid}&employeeId=${employeeIdList[selected[0]]}&storeId=${storeIdList[selected[0]]}")
                    .then((value) => {
                TDToast.showText('添加成功', context: context),
                _getProjectEmployeeList()
                });
              }, data: [employeeList]);
        }
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _getProjectEmployeeList();
    });
  }
  _getProjectEmployeeList () async {
    var authorization = ScopedModelHelper.getModel<AuthModel>(context).token;
    var response = await DioHttp().init(context, authorization).get("/oms/project-employee/${widget.pkid}");
    List<Widget > temp = [];
    setState(() {
      employeeList = [];
      list = [const Center(
        child: TDLoading(
          size: TDLoadingSize.large,
          icon: TDLoadingIcon.circle,
        ),
      )];
      employeeIdList = [];
      storeIdList = [];
    });
    for (var item in response){
       temp.add(Container(
         padding: EdgeInsets.all(20),
         margin: EdgeInsets.all(5),
         decoration: BoxDecoration(
           color: Color.fromRGBO(200, 200, 200, 0.3),
           borderRadius: BorderRadius.circular(10),
         ),
         child: Row(
           mainAxisAlignment: MainAxisAlignment.spaceBetween,
           children: [
             Text(item['employeeName']),
             TDButton(
               text: '删除',
               size: TDButtonSize.large,
               type: TDButtonType.text,
               shape: TDButtonShape.rectangle,
               theme: TDButtonTheme.danger,
               onTap: (){
                 _deleteProjectEmployee(item['pkid'],item["projectId"],authorization);
               }
             )
           ]
         ),
       ));
    }
    var employees = await DioHttp().init(context, authorization).get("/uc/employee/list?page=1&size=9999");

    for (var item in employees['records']){
      employeeList.add(item['name']);
      employeeIdList.add(item['pkid']);
      storeIdList.add(item['storeId']);
    }
    setState(() {
      list = temp;
      employeeList = employeeList;
    });
  }

  _deleteProjectEmployee(pkid,projectId,authorization) async{
    await DioHttp().init(context,authorization).delete("/oms/project-employee/del${pkid}/${projectId}");
    _getProjectEmployeeList();
  }

}